package com.example.medical.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.medical.entity.dto.*;
import com.example.medical.entity.pojo.*;
import com.example.medical.mapper.*;
import com.example.medical.service.*;
import com.example.medical.utils.common.enums.Code;
import com.example.medical.utils.common.result.PageResponseResult;
import com.example.medical.utils.common.result.Result;
import com.example.medical.utils.config.BaseContext;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
* @author 24354
* @description 针对表【affairs】的数据库操作Service实现
* @createDate 2024-04-11 02:18:43
*/
@Service
public class AffairsServiceImpl extends ServiceImpl<AffairsMapper, Affairs>
    implements AffairsService{

    @Autowired
    private AffairsMapper affairsMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DeviceMapper deviceMapper;
    @Autowired
    private StoreroomMapper storeroomMapper;
    @Autowired
    private LogingService logingService;
    @Autowired
    private LeadershipMapper leadershipMapper;
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private AdminService adminService;

    @Override
    public Result UserGetAffairsGetSetList(GetAffairsListActionDto dto) {
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        dto.checkParam();
        IPage<Affairs> page=new Page<>(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<Affairs> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Affairs::getAffairsCategory,dto.getCategory()).eq(Affairs::getAffairsUserId,BaseContext.getCurrentId());
        wrapper.or().eq(Affairs::getAffairsCategory,dto.getCategory()).eq(Affairs::getAffairsEndUserId,BaseContext.getCurrentId());
        if (dto.getType()!=null) {
            wrapper.eq(Affairs::getAffairsType,dto.getType());
        }
        if (dto.getDeviceId()!=null) {
            wrapper.eq(Affairs::getAffairsDeviceId,dto.getDeviceId());
        }
        if (dto.getDeviceTypeId()!=null) {
            wrapper.eq(Affairs::getAffairsDeviceTypeId,dto.getDeviceTypeId());
        }
        if (StringUtils.isNotBlank(dto.getName())) {
            wrapper.like(Affairs::getAffairsUserNick,dto.getName());
        }
        page=page(page,wrapper);
        Result result=new PageResponseResult(dto.getPage(),dto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());
        return result;
    }

    @Override
    public Result UserGetAffairsList(GetAffairsListDto dto) {
        Long currentId = BaseContext.getCurrentId();
        if (currentId==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        dto.checkParam();
        IPage<Affairs> page=new Page<>(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<Affairs> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(Affairs::getAffairsCategory,dto.getCategory()).eq(Affairs::getAffairsUserId,BaseContext.getCurrentId());
        if (dto.getType()!=null) {
            wrapper.eq(Affairs::getAffairsType,dto.getType());
        }
        if (StringUtils.isNotBlank(dto.getDeviceName())) {
            wrapper.like(Affairs::getAffairsDeviceName,dto.getDeviceName());
        }
        if (dto.getDeviceTypeId()!=null) {
            wrapper.eq(Affairs::getAffairsDeviceTypeId,dto.getDeviceTypeId());
        }
        if (StringUtils.isNotBlank(dto.getUserName())) {
            wrapper.like(Affairs::getAffairsUserNick,dto.getUserName());
        }
        page=page(page,wrapper);
        Result result=new PageResponseResult(dto.getPage(),dto.getSize(), (int) page.getTotal());
        result.setData(page.getRecords());
        return result;
    }

    @Override
        public Result UserSaveAffairs(UserSaveAffairsDto dto) {

            if (dto==null) {
                return Result.errorResult(Code.PARAM_REQUIRE);
            }
            Long userId = BaseContext.getCurrentId();
            User checkUser = userMapper.selectById(userId);
            if (userId==null||checkUser==null) {
                return Result.errorResult(Code.NEED_LOGIN);
            }
            Device checkDevice = deviceMapper.selectById(dto.getDeviceId());
            if (checkDevice==null) {
                return Result.errorResult(Code.DATA_NOT_EXIST);
            }

        Affairs affairs=new Affairs();
            if (dto.getCategory()==0) {
                User checkUser1 = userMapper.selectById(dto.getUserId());
                if (checkUser1==null) {
                    return Result.errorResult(Code.DATA_NOT_EXIST);
                }
                Affairs affairs1=new Affairs();
                //设备出库
                affairs= affairs.builder()
                        .affairsCategory(0)
                        .affairsDeviceName(checkDevice.getDeviceName())
                        .affairsDeviceNumber(checkDevice.getDeviceNumber())
                        .affairsDeviceModel(checkDevice.getDeviceModel())
                        .affairsDeviceInventory(checkDevice.getDeviceInventory())
                        .affairsDeviceId(checkDevice.getDeviceId())
                        .affairsDeviceTypeName(checkDevice.getDeviceTypeName())
                        .affairsDeviceTypeId(checkDevice.getDeviceTypeId())
                        .affairsDivisionName(checkDevice.getDeviceDivisionName())
                        .affairsDivisionId(checkDevice.getDeviceDivisionId())
                        .affairsUserNick(checkUser.getUserNick())
                        .affairsUserImgUrl(checkUser.getUserImgUrl())
                        .affairsUserDivisionName(checkUser.getUserDivisionName())
                        .affairsUserDivisionId(checkUser.getUserDivisionId())
                        .affairsUserPhone(checkUser.getUserPhone())
                        .affairsUserNumber(checkUser.getUserNumber())
                        .affairsUserEmail(checkUser.getUserEmail())
                        .affairsUserId(checkUser.getUserId())
                        .affairsNumber(dto.getNumber())
                        .affairsMessage(dto.getMemo())
                        .affairsEndUserNick(checkUser1.getUserNick())
                        .affairsEndUserImgUrl(checkUser1.getUserImgUrl())
                        .affairsEndUserDivisionName(checkUser1.getUserDivisionName())
                        .affairsEndUserDivisionId(checkUser1.getUserDivisionId())
                        .affairsEndUserPhone(checkUser1.getUserPhone())
                        .affairsEndUserNumber(checkUser1.getUserNumber())
                        .affairsEndUserEmail(checkUser1.getUserEmail())
                        .affairsEndUserId(checkUser1.getUserId())
                        .affairsAction(0)
                        .saveTime(String.valueOf(LocalDateTime.now()))
                        .build();
                //设备入库
                affairs1= affairs1.builder()
                        .affairsCategory(1)
                        .affairsDeviceName(checkDevice.getDeviceName())
                        .affairsDeviceNumber(checkDevice.getDeviceNumber())
                        .affairsDeviceModel(checkDevice.getDeviceModel())
                        .affairsDeviceInventory(checkDevice.getDeviceInventory())
                        .affairsDeviceId(checkDevice.getDeviceId())
                        .affairsDeviceTypeName(checkDevice.getDeviceTypeName())
                        .affairsDeviceTypeId(checkDevice.getDeviceTypeId())
                        .affairsDivisionName(checkDevice.getDeviceDivisionName())
                        .affairsDivisionId(checkDevice.getDeviceDivisionId())
                        .affairsUserNick(checkUser.getUserNick())
                        .affairsUserImgUrl(checkUser.getUserImgUrl())
                        .affairsUserDivisionName(checkUser.getUserDivisionName())
                        .affairsUserDivisionId(checkUser.getUserDivisionId())
                        .affairsUserPhone(checkUser.getUserPhone())
                        .affairsUserNumber(checkUser.getUserNumber())
                        .affairsUserEmail(checkUser.getUserEmail())
                        .affairsUserId(checkUser.getUserId())
                        .affairsNumber(dto.getNumber())
                        .affairsMessage(dto.getMemo())
                        .affairsEndUserNick(checkUser1.getUserNick())
                        .affairsEndUserImgUrl(checkUser1.getUserImgUrl())
                        .affairsEndUserDivisionName(checkUser1.getUserDivisionName())
                        .affairsEndUserDivisionId(checkUser1.getUserDivisionId())
                        .affairsEndUserPhone(checkUser1.getUserPhone())
                        .affairsEndUserNumber(checkUser1.getUserNumber())
                        .affairsEndUserEmail(checkUser1.getUserEmail())
                        .affairsEndUserId(checkUser1.getUserId())
                        .affairsAction(1)
                        .saveTime(String.valueOf(LocalDateTime.now()))
                        .build();
                boolean checkSave = affairsMapper.saveReturnIdByGetSet(affairs);
                boolean checkSave1 = affairsMapper.saveReturnIdByGetSet(affairs1);
                boolean checkSaveLoging = logingService.SaveLoging(affairs, checkUser);
                boolean checkSaveLoging1 = logingService.SaveLoging(affairs1, checkUser);
                if (!checkSave||!checkSave1||!checkSaveLoging||!checkSaveLoging1) {
                    return Result.errorResult(Code.SERVER_ERROR);
                }
                return Result.okResult(Code.SUCCESS);
            }
            if (dto.getCategory()==1) {
                User checkUser1 = userMapper.selectById(dto.getUserId());
                if (checkUser1==null) {
                    return Result.errorResult(Code.DATA_NOT_EXIST);
                }
                Affairs affairs1=new Affairs();
                //设备入库
                affairs= affairs.builder()
                        .affairsCategory(1)
                        .affairsDeviceName(checkDevice.getDeviceName())
                        .affairsDeviceNumber(checkDevice.getDeviceNumber())
                        .affairsDeviceModel(checkDevice.getDeviceModel())
                        .affairsDeviceInventory(checkDevice.getDeviceInventory())
                        .affairsDeviceId(checkDevice.getDeviceId())
                        .affairsDeviceTypeName(checkDevice.getDeviceTypeName())
                        .affairsDeviceTypeId(checkDevice.getDeviceTypeId())
                        .affairsDivisionName(checkDevice.getDeviceDivisionName())
                        .affairsDivisionId(checkDevice.getDeviceDivisionId())
                        .affairsUserNick(checkUser.getUserNick())
                        .affairsUserImgUrl(checkUser.getUserImgUrl())
                        .affairsUserDivisionName(checkUser.getUserDivisionName())
                        .affairsUserDivisionId(checkUser.getUserDivisionId())
                        .affairsUserPhone(checkUser.getUserPhone())
                        .affairsUserNumber(checkUser.getUserNumber())
                        .affairsUserEmail(checkUser.getUserEmail())
                        .affairsUserId(checkUser.getUserId())
                        .affairsNumber(dto.getNumber())
                        .affairsMessage(dto.getMemo())
                        .affairsEndUserNick(checkUser1.getUserNick())
                        .affairsEndUserImgUrl(checkUser1.getUserImgUrl())
                        .affairsEndUserDivisionName(checkUser1.getUserDivisionName())
                        .affairsEndUserDivisionId(checkUser1.getUserDivisionId())
                        .affairsEndUserPhone(checkUser1.getUserPhone())
                        .affairsEndUserNumber(checkUser1.getUserNumber())
                        .affairsEndUserEmail(checkUser1.getUserEmail())
                        .affairsEndUserId(checkUser1.getUserId())
                        .affairsAction(1)
                        .saveTime(String.valueOf(LocalDateTime.now()))
                        .build();
                //设备出库
                affairs1= affairs1.builder()
                        .affairsCategory(0)
                        .affairsDeviceName(checkDevice.getDeviceName())
                        .affairsDeviceNumber(checkDevice.getDeviceNumber())
                        .affairsDeviceModel(checkDevice.getDeviceModel())
                        .affairsDeviceInventory(checkDevice.getDeviceInventory())
                        .affairsDeviceId(checkDevice.getDeviceId())
                        .affairsDeviceTypeName(checkDevice.getDeviceTypeName())
                        .affairsDeviceTypeId(checkDevice.getDeviceTypeId())
                        .affairsDivisionName(checkDevice.getDeviceDivisionName())
                        .affairsDivisionId(checkDevice.getDeviceDivisionId())
                        .affairsUserNick(checkUser.getUserNick())
                        .affairsUserImgUrl(checkUser.getUserImgUrl())
                        .affairsUserDivisionName(checkUser.getUserDivisionName())
                        .affairsUserDivisionId(checkUser.getUserDivisionId())
                        .affairsUserPhone(checkUser.getUserPhone())
                        .affairsUserNumber(checkUser.getUserNumber())
                        .affairsUserEmail(checkUser.getUserEmail())
                        .affairsUserId(checkUser.getUserId())
                        .affairsNumber(dto.getNumber())
                        .affairsMessage(dto.getMemo())
                        .affairsEndUserNick(checkUser1.getUserNick())
                        .affairsEndUserImgUrl(checkUser1.getUserImgUrl())
                        .affairsEndUserDivisionName(checkUser1.getUserDivisionName())
                        .affairsEndUserDivisionId(checkUser1.getUserDivisionId())
                        .affairsEndUserPhone(checkUser1.getUserPhone())
                        .affairsEndUserNumber(checkUser1.getUserNumber())
                        .affairsEndUserEmail(checkUser1.getUserEmail())
                        .affairsEndUserId(checkUser1.getUserId())
                        .affairsAction(0)
                        .saveTime(String.valueOf(LocalDateTime.now()))
                        .build();
                boolean checkSave = affairsMapper.saveReturnIdByGetSet(affairs);
                boolean checkSave1 = affairsMapper.saveReturnIdByGetSet(affairs1);
                boolean checkSaveLoging = logingService.SaveLoging(affairs, checkUser);
                boolean checkSaveLoging1 = logingService.SaveLoging(affairs1, checkUser);
                if (!checkSave||!checkSave1||!checkSaveLoging||!checkSaveLoging1) {
                    return Result.errorResult(Code.SERVER_ERROR);
                }
                return Result.okResult(Code.SUCCESS);
            }

            if (dto.getCategory()==3) {
                //设备报损
                affairs= affairs.builder()
                        .affairsCategory(dto.getCategory())
                        .affairsDeviceName(checkDevice.getDeviceName())
                        .affairsDeviceNumber(checkDevice.getDeviceNumber())
                        .affairsDeviceModel(checkDevice.getDeviceModel())
                        .affairsDeviceInventory(checkDevice.getDeviceInventory())
                        .affairsDeviceId(checkDevice.getDeviceId())
                        .affairsDeviceTypeName(checkDevice.getDeviceTypeName())
                        .affairsDeviceTypeId(checkDevice.getDeviceTypeId())
                        .affairsDivisionName(checkDevice.getDeviceDivisionName())
                        .affairsDivisionId(checkDevice.getDeviceDivisionId())
                        .affairsUserNick(checkUser.getUserNick())
                        .affairsUserImgUrl(checkUser.getUserImgUrl())
                        .affairsUserDivisionName(checkUser.getUserDivisionName())
                        .affairsUserDivisionId(checkUser.getUserDivisionId())
                        .affairsUserPhone(checkUser.getUserPhone())
                        .affairsUserNumber(checkUser.getUserNumber())
                        .affairsUserEmail(checkUser.getUserEmail())
                        .affairsUserId(checkUser.getUserId())
                        .affairsNumber(dto.getNumber())
                        .affairsMessage(dto.getMemo())
                        .saveTime(String.valueOf(LocalDateTime.now()))
                        .build();
                boolean checkSave = affairsMapper.saveReturnId(affairs);
                boolean checkSaveLoging = logingService.SaveLoging(affairs, checkUser);
                if (!checkSave||!checkSaveLoging) {
                    return Result.errorResult(Code.SERVER_ERROR);
                }
                return Result.okResult(Code.SUCCESS);
            }
            if (dto.getCategory()==4) {
                //设备检测
                affairs= affairs.builder()
                        .affairsCategory(dto.getCategory())
                        .affairsDeviceName(checkDevice.getDeviceName())
                        .affairsDeviceNumber(checkDevice.getDeviceNumber())
                        .affairsDeviceModel(checkDevice.getDeviceModel())
                        .affairsDeviceInventory(checkDevice.getDeviceInventory())
                        .affairsDeviceId(checkDevice.getDeviceId())
                        .affairsDeviceTypeName(checkDevice.getDeviceTypeName())
                        .affairsDeviceTypeId(checkDevice.getDeviceTypeId())
                        .affairsDivisionName(checkDevice.getDeviceDivisionName())
                        .affairsDivisionId(checkDevice.getDeviceDivisionId())
                        .affairsUserNick(checkUser.getUserNick())
                        .affairsUserImgUrl(checkUser.getUserImgUrl())
                        .affairsUserDivisionName(checkUser.getUserDivisionName())
                        .affairsUserDivisionId(checkUser.getUserDivisionId())
                        .affairsUserPhone(checkUser.getUserPhone())
                        .affairsUserNumber(checkUser.getUserNumber())
                        .affairsUserEmail(checkUser.getUserEmail())
                        .affairsUserId(checkUser.getUserId())
                        .affairsNumber(dto.getNumber())
                        .affairsMessage(dto.getMemo())
                        .saveTime(String.valueOf(LocalDateTime.now()))
                        .build();
                boolean checkSave = affairsMapper.saveReturnId(affairs);
                boolean checkSaveLoging = logingService.SaveLoging(affairs, checkUser);
                if (!checkSave||!checkSaveLoging) {
                    return Result.errorResult(Code.SERVER_ERROR);
                }
                return Result.okResult(Code.SUCCESS);
            }
            if (dto.getCategory()==5) {
                //设备维修
                affairs= affairs.builder()
                        .affairsCategory(dto.getCategory())
                        .affairsDeviceName(checkDevice.getDeviceName())
                        .affairsDeviceNumber(checkDevice.getDeviceNumber())
                        .affairsDeviceModel(checkDevice.getDeviceModel())
                        .affairsDeviceInventory(checkDevice.getDeviceInventory())
                        .affairsDeviceId(checkDevice.getDeviceId())
                        .affairsDeviceTypeName(checkDevice.getDeviceTypeName())
                        .affairsDeviceTypeId(checkDevice.getDeviceTypeId())
                        .affairsDivisionName(checkDevice.getDeviceDivisionName())
                        .affairsDivisionId(checkDevice.getDeviceDivisionId())
                        .affairsUserNick(checkUser.getUserNick())
                        .affairsUserImgUrl(checkUser.getUserImgUrl())
                        .affairsUserDivisionName(checkUser.getUserDivisionName())
                        .affairsUserDivisionId(checkUser.getUserDivisionId())
                        .affairsUserPhone(checkUser.getUserPhone())
                        .affairsUserNumber(checkUser.getUserNumber())
                        .affairsUserEmail(checkUser.getUserEmail())
                        .affairsUserId(checkUser.getUserId())
                        .affairsNumber(dto.getNumber())
                        .affairsMessage(dto.getMemo())
                        .saveTime(String.valueOf(LocalDateTime.now()))
                        .build();
                boolean checkSave = affairsMapper.saveReturnId(affairs);
                boolean checkSaveLoging = logingService.SaveLoging(affairs, checkUser);
                if (!checkSave||!checkSaveLoging) {
                    return Result.errorResult(Code.SERVER_ERROR);
                }
                return Result.okResult(Code.SUCCESS);
            }
            if (dto.getCategory()==6) {
                //设备采购
                affairs=affairs.builder()
                        .affairsCategory(dto.getCategory())
                        .affairsDeviceName(checkDevice.getDeviceName())
                        .affairsDeviceNumber(checkDevice.getDeviceNumber())
                        .affairsDeviceModel(checkDevice.getDeviceModel())
                        .affairsDeviceInventory(checkDevice.getDeviceInventory())
                        .affairsDeviceId(checkDevice.getDeviceId())
                        .affairsDeviceTypeName(checkDevice.getDeviceTypeName())
                        .affairsDeviceTypeId(checkDevice.getDeviceTypeId())
                        .affairsDivisionName(checkDevice.getDeviceDivisionName())
                        .affairsDivisionId(checkDevice.getDeviceDivisionId())
                        .affairsUserNick(checkUser.getUserNick())
                        .affairsUserImgUrl(checkUser.getUserImgUrl())
                        .affairsUserDivisionName(checkUser.getUserDivisionName())
                        .affairsUserDivisionId(checkUser.getUserDivisionId())
                        .affairsUserPhone(checkUser.getUserPhone())
                        .affairsUserNumber(checkUser.getUserNumber())
                        .affairsUserEmail(checkUser.getUserEmail())
                        .affairsUserId(checkUser.getUserId())
                        .affairsNumber(dto.getNumber())
                        .affairsMessage(dto.getMemo())
                        .saveTime(String.valueOf(LocalDateTime.now()))
                        .build();
                boolean checkSave = affairsMapper.saveReturnId(affairs);
                boolean checkSaveLoging = logingService.SaveLoging(affairs, checkUser);
                if (!checkSave||!checkSaveLoging) {
                    return Result.errorResult(Code.SERVER_ERROR);
                }
                return Result.okResult(Code.SUCCESS);
            }
//            boolean checkSaveLoging = logingService.SaveLoging(affairs, checkUser);
//            boolean checkSave = save(affairs);
//            if (!checkSave||!checkSaveLoging) {
//                return Result.errorResult(Code.SERVER_ERROR);
//            }
//            return Result.okResult(Code.SUCCESS);
        return Result.errorResult(Code.PARAM_INVALID);
        }

    @Override
    public Result AdminSaveAffairs(AdminSaveAffairsDto dto) {
        if (dto==null) {
            return Result.errorResult(Code.PARAM_REQUIRE);
        }
        Admin checkAdmin = adminService.getById(BaseContext.getCurrentId());
        if (checkAdmin==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        User checkUser = userMapper.selectById(dto.getUserId());
        if (checkUser==null) {
            return Result.errorResult(Code.DATA_NOT_EXIST);
        }
        Device checkDevice = deviceMapper.selectById(dto.getDeviceId());
        if (checkDevice==null) {
            return Result.errorResult(Code.DATA_NOT_EXIST);
        }
        Affairs affairs=new Affairs();
        if (dto.getCategory()==0) {
            User checkUser1 = userMapper.selectById(dto.getUserId());
            if (checkUser1==null) {
                return Result.errorResult(Code.DATA_NOT_EXIST);
            }
            Affairs affairs1=new Affairs();
            //设备出库
            affairs= affairs.builder()
                    .affairsCategory(0)
                    .affairsDeviceName(checkDevice.getDeviceName())
                    .affairsDeviceNumber(checkDevice.getDeviceNumber())
                    .affairsDeviceModel(checkDevice.getDeviceModel())
                    .affairsDeviceInventory(checkDevice.getDeviceInventory())
                    .affairsDeviceId(checkDevice.getDeviceId())
                    .affairsDeviceTypeName(checkDevice.getDeviceTypeName())
                    .affairsDeviceTypeId(checkDevice.getDeviceTypeId())
                    .affairsDivisionName(checkDevice.getDeviceDivisionName())
                    .affairsDivisionId(checkDevice.getDeviceDivisionId())
                    .affairsUserNick(checkUser.getUserNick())
                    .affairsUserImgUrl(checkUser.getUserImgUrl())
                    .affairsUserDivisionName(checkUser.getUserDivisionName())
                    .affairsUserDivisionId(checkUser.getUserDivisionId())
                    .affairsUserPhone(checkUser.getUserPhone())
                    .affairsUserNumber(checkUser.getUserNumber())
                    .affairsUserEmail(checkUser.getUserEmail())
                    .affairsUserId(checkUser.getUserId())
                    .affairsNumber(dto.getNumber())
                    .affairsMessage(dto.getMemo())
                    .affairsEndUserNick(checkUser1.getUserNick())
                    .affairsEndUserImgUrl(checkUser1.getUserImgUrl())
                    .affairsEndUserDivisionName(checkUser1.getUserDivisionName())
                    .affairsEndUserDivisionId(checkUser1.getUserDivisionId())
                    .affairsEndUserPhone(checkUser1.getUserPhone())
                    .affairsEndUserNumber(checkUser1.getUserNumber())
                    .affairsEndUserEmail(checkUser1.getUserEmail())
                    .affairsEndUserId(checkUser1.getUserId())
                    .affairsAction(0)
                    .saveTime(String.valueOf(LocalDateTime.now()))
                    .build();
            //设备入库
            affairs= affairs.builder()
                    .affairsCategory(1)
                    .affairsDeviceName(checkDevice.getDeviceName())
                    .affairsDeviceNumber(checkDevice.getDeviceNumber())
                    .affairsDeviceModel(checkDevice.getDeviceModel())
                    .affairsDeviceInventory(checkDevice.getDeviceInventory())
                    .affairsDeviceId(checkDevice.getDeviceId())
                    .affairsDeviceTypeName(checkDevice.getDeviceTypeName())
                    .affairsDeviceTypeId(checkDevice.getDeviceTypeId())
                    .affairsDivisionName(checkDevice.getDeviceDivisionName())
                    .affairsDivisionId(checkDevice.getDeviceDivisionId())
                    .affairsUserNick(checkUser.getUserNick())
                    .affairsUserImgUrl(checkUser.getUserImgUrl())
                    .affairsUserDivisionName(checkUser.getUserDivisionName())
                    .affairsUserDivisionId(checkUser.getUserDivisionId())
                    .affairsUserPhone(checkUser.getUserPhone())
                    .affairsUserNumber(checkUser.getUserNumber())
                    .affairsUserEmail(checkUser.getUserEmail())
                    .affairsUserId(checkUser.getUserId())
                    .affairsNumber(dto.getNumber())
                    .affairsMessage(dto.getMemo())
                    .affairsEndUserNick(checkUser1.getUserNick())
                    .affairsEndUserImgUrl(checkUser1.getUserImgUrl())
                    .affairsEndUserDivisionName(checkUser1.getUserDivisionName())
                    .affairsEndUserDivisionId(checkUser1.getUserDivisionId())
                    .affairsEndUserPhone(checkUser1.getUserPhone())
                    .affairsEndUserNumber(checkUser1.getUserNumber())
                    .affairsEndUserEmail(checkUser1.getUserEmail())
                    .affairsEndUserId(checkUser1.getUserId())
                    .affairsAction(1)
                    .saveTime(String.valueOf(LocalDateTime.now()))
                    .build();
            boolean checkSave = affairsMapper.saveReturnIdByGetSet(affairs);
            boolean checkSave1 = affairsMapper.saveReturnIdByGetSet(affairs1);
            boolean checkSaveLoging = logingService.SaveLoging(affairs, checkUser);
            boolean checkSaveLoging1 = logingService.SaveLoging(affairs1, checkUser);
            if (!checkSave||!checkSave1||!checkSaveLoging||!checkSaveLoging1) {
                return Result.errorResult(Code.SERVER_ERROR);
            }
            return Result.okResult(Code.SUCCESS);
        }
        if (dto.getCategory()==1) {
            User checkUser1 = userMapper.selectById(dto.getUserId());
            if (checkUser1==null) {
                return Result.errorResult(Code.DATA_NOT_EXIST);
            }
            Affairs affairs1=new Affairs();
            //设备入库
            affairs= affairs.builder()
                    .affairsCategory(1)
                    .affairsDeviceName(checkDevice.getDeviceName())
                    .affairsDeviceNumber(checkDevice.getDeviceNumber())
                    .affairsDeviceModel(checkDevice.getDeviceModel())
                    .affairsDeviceInventory(checkDevice.getDeviceInventory())
                    .affairsDeviceId(checkDevice.getDeviceId())
                    .affairsDeviceTypeName(checkDevice.getDeviceTypeName())
                    .affairsDeviceTypeId(checkDevice.getDeviceTypeId())
                    .affairsDivisionName(checkDevice.getDeviceDivisionName())
                    .affairsDivisionId(checkDevice.getDeviceDivisionId())
                    .affairsUserNick(checkUser.getUserNick())
                    .affairsUserImgUrl(checkUser.getUserImgUrl())
                    .affairsUserDivisionName(checkUser.getUserDivisionName())
                    .affairsUserDivisionId(checkUser.getUserDivisionId())
                    .affairsUserPhone(checkUser.getUserPhone())
                    .affairsUserNumber(checkUser.getUserNumber())
                    .affairsUserEmail(checkUser.getUserEmail())
                    .affairsUserId(checkUser.getUserId())
                    .affairsNumber(dto.getNumber())
                    .affairsMessage(dto.getMemo())
                    .affairsEndUserNick(checkUser1.getUserNick())
                    .affairsEndUserImgUrl(checkUser1.getUserImgUrl())
                    .affairsEndUserDivisionName(checkUser1.getUserDivisionName())
                    .affairsEndUserDivisionId(checkUser1.getUserDivisionId())
                    .affairsEndUserPhone(checkUser1.getUserPhone())
                    .affairsEndUserNumber(checkUser1.getUserNumber())
                    .affairsEndUserEmail(checkUser1.getUserEmail())
                    .affairsEndUserId(checkUser1.getUserId())
                    .affairsAction(1)
                    .saveTime(String.valueOf(LocalDateTime.now()))
                    .build();

            //设备出库
            affairs1= affairs1.builder()
                    .affairsCategory(0)
                    .affairsDeviceName(checkDevice.getDeviceName())
                    .affairsDeviceNumber(checkDevice.getDeviceNumber())
                    .affairsDeviceModel(checkDevice.getDeviceModel())
                    .affairsDeviceInventory(checkDevice.getDeviceInventory())
                    .affairsDeviceId(checkDevice.getDeviceId())
                    .affairsDeviceTypeName(checkDevice.getDeviceTypeName())
                    .affairsDeviceTypeId(checkDevice.getDeviceTypeId())
                    .affairsDivisionName(checkDevice.getDeviceDivisionName())
                    .affairsDivisionId(checkDevice.getDeviceDivisionId())
                    .affairsUserNick(checkUser.getUserNick())
                    .affairsUserImgUrl(checkUser.getUserImgUrl())
                    .affairsUserDivisionName(checkUser.getUserDivisionName())
                    .affairsUserDivisionId(checkUser.getUserDivisionId())
                    .affairsUserPhone(checkUser.getUserPhone())
                    .affairsUserNumber(checkUser.getUserNumber())
                    .affairsUserEmail(checkUser.getUserEmail())
                    .affairsUserId(checkUser.getUserId())
                    .affairsNumber(dto.getNumber())
                    .affairsMessage(dto.getMemo())
                    .affairsEndUserNick(checkUser1.getUserNick())
                    .affairsEndUserImgUrl(checkUser1.getUserImgUrl())
                    .affairsEndUserDivisionName(checkUser1.getUserDivisionName())
                    .affairsEndUserDivisionId(checkUser1.getUserDivisionId())
                    .affairsEndUserPhone(checkUser1.getUserPhone())
                    .affairsEndUserNumber(checkUser1.getUserNumber())
                    .affairsEndUserEmail(checkUser1.getUserEmail())
                    .affairsEndUserId(checkUser1.getUserId())
                    .affairsAction(0)
                    .saveTime(String.valueOf(LocalDateTime.now()))
                    .build();
            boolean checkSave = affairsMapper.saveReturnIdByGetSet(affairs);
            boolean checkSave1 = affairsMapper.saveReturnIdByGetSet(affairs1);
            boolean checkSaveLoging = logingService.SaveLoging(affairs, checkUser);
            boolean checkSaveLoging1 = logingService.SaveLoging(affairs1, checkUser);
            if (!checkSave||!checkSave1||!checkSaveLoging||!checkSaveLoging1) {
                return Result.errorResult(Code.SERVER_ERROR);
            }
            return Result.okResult(Code.SUCCESS);
        }

        if (dto.getCategory()==3) {
            //设备报损
            affairs= affairs.builder()
                    .affairsDeviceName(checkDevice.getDeviceName())
                    .affairsDeviceNumber(checkDevice.getDeviceNumber())
                    .affairsDeviceModel(checkDevice.getDeviceModel())
                    .affairsDeviceInventory(checkDevice.getDeviceInventory())
                    .affairsDeviceId(checkDevice.getDeviceId())
                    .affairsDeviceTypeName(checkDevice.getDeviceTypeName())
                    .affairsDeviceTypeId(checkDevice.getDeviceTypeId())
                    .affairsDivisionName(checkDevice.getDeviceDivisionName())
                    .affairsDivisionId(checkDevice.getDeviceDivisionId())
                    .affairsUserNick(checkUser.getUserNick())
                    .affairsUserImgUrl(checkUser.getUserImgUrl())
                    .affairsUserDivisionName(checkUser.getUserDivisionName())
                    .affairsUserDivisionId(checkUser.getUserDivisionId())
                    .affairsUserPhone(checkUser.getUserPhone())
                    .affairsUserNumber(checkUser.getUserNumber())
                    .affairsUserEmail(checkUser.getUserEmail())
                    .affairsUserId(checkUser.getUserId())
                    .affairsNumber(dto.getNumber())
                    .affairsMessage(dto.getMemo())
                    .saveTime(String.valueOf(LocalDateTime.now()))
                    .build();
            boolean checkSave = affairsMapper.saveReturnId(affairs);
            boolean checkSaveLoging = logingService.SaveLoging(affairs, checkUser);
            if (!checkSave||!checkSaveLoging) {
                return Result.errorResult(Code.SERVER_ERROR);
            }
            return Result.okResult(Code.SUCCESS);
        }
        if (dto.getCategory()==4) {
            //设备检测
            affairs= affairs.builder()
                    .affairsDeviceName(checkDevice.getDeviceName())
                    .affairsDeviceNumber(checkDevice.getDeviceNumber())
                    .affairsDeviceModel(checkDevice.getDeviceModel())
                    .affairsDeviceInventory(checkDevice.getDeviceInventory())
                    .affairsDeviceId(checkDevice.getDeviceId())
                    .affairsDeviceTypeName(checkDevice.getDeviceTypeName())
                    .affairsDeviceTypeId(checkDevice.getDeviceTypeId())
                    .affairsDivisionName(checkDevice.getDeviceDivisionName())
                    .affairsDivisionId(checkDevice.getDeviceDivisionId())
                    .affairsUserNick(checkUser.getUserNick())
                    .affairsUserImgUrl(checkUser.getUserImgUrl())
                    .affairsUserDivisionName(checkUser.getUserDivisionName())
                    .affairsUserDivisionId(checkUser.getUserDivisionId())
                    .affairsUserPhone(checkUser.getUserPhone())
                    .affairsUserNumber(checkUser.getUserNumber())
                    .affairsUserEmail(checkUser.getUserEmail())
                    .affairsUserId(checkUser.getUserId())
                    .affairsNumber(dto.getNumber())
                    .affairsMessage(dto.getMemo())
                    .saveTime(String.valueOf(LocalDateTime.now()))
                    .build();
            boolean checkSave = affairsMapper.saveReturnId(affairs);
            boolean checkSaveLoging = logingService.SaveLoging(affairs, checkUser);
            if (!checkSave||!checkSaveLoging) {
                return Result.errorResult(Code.SERVER_ERROR);
            }
            return Result.okResult(Code.SUCCESS);
        }
        if (dto.getCategory()==5) {
            //设备维修
            affairs= affairs.builder()
                    .affairsDeviceName(checkDevice.getDeviceName())
                    .affairsDeviceNumber(checkDevice.getDeviceNumber())
                    .affairsDeviceModel(checkDevice.getDeviceModel())
                    .affairsDeviceInventory(checkDevice.getDeviceInventory())
                    .affairsDeviceId(checkDevice.getDeviceId())
                    .affairsDeviceTypeName(checkDevice.getDeviceTypeName())
                    .affairsDeviceTypeId(checkDevice.getDeviceTypeId())
                    .affairsDivisionName(checkDevice.getDeviceDivisionName())
                    .affairsDivisionId(checkDevice.getDeviceDivisionId())
                    .affairsUserNick(checkUser.getUserNick())
                    .affairsUserImgUrl(checkUser.getUserImgUrl())
                    .affairsUserDivisionName(checkUser.getUserDivisionName())
                    .affairsUserDivisionId(checkUser.getUserDivisionId())
                    .affairsUserPhone(checkUser.getUserPhone())
                    .affairsUserNumber(checkUser.getUserNumber())
                    .affairsUserEmail(checkUser.getUserEmail())
                    .affairsUserId(checkUser.getUserId())
                    .affairsNumber(dto.getNumber())
                    .affairsMessage(dto.getMemo())
                    .saveTime(String.valueOf(LocalDateTime.now()))
                    .build();
            boolean checkSave = affairsMapper.saveReturnId(affairs);
            boolean checkSaveLoging = logingService.SaveLoging(affairs, checkUser);
            if (!checkSave||!checkSaveLoging) {
                return Result.errorResult(Code.SERVER_ERROR);
            }
            return Result.okResult(Code.SUCCESS);
        }
        if (dto.getCategory()==6) {
            //设备采购
            affairs=affairs.builder()
                    .affairsDeviceName(checkDevice.getDeviceName())
                    .affairsDeviceNumber(checkDevice.getDeviceNumber())
                    .affairsDeviceModel(checkDevice.getDeviceModel())
                    .affairsDeviceInventory(checkDevice.getDeviceInventory())
                    .affairsDeviceId(checkDevice.getDeviceId())
                    .affairsDeviceTypeName(checkDevice.getDeviceTypeName())
                    .affairsDeviceTypeId(checkDevice.getDeviceTypeId())
                    .affairsDivisionName(checkDevice.getDeviceDivisionName())
                    .affairsDivisionId(checkDevice.getDeviceDivisionId())
                    .affairsUserNick(checkUser.getUserNick())
                    .affairsUserImgUrl(checkUser.getUserImgUrl())
                    .affairsUserDivisionName(checkUser.getUserDivisionName())
                    .affairsUserDivisionId(checkUser.getUserDivisionId())
                    .affairsUserPhone(checkUser.getUserPhone())
                    .affairsUserNumber(checkUser.getUserNumber())
                    .affairsUserEmail(checkUser.getUserEmail())
                    .affairsUserId(checkUser.getUserId())
                    .affairsNumber(dto.getNumber())
                    .affairsMessage(dto.getMemo())
                    .saveTime(String.valueOf(LocalDateTime.now()))
                    .build();
            boolean checkSave = affairsMapper.saveReturnId(affairs);
            boolean checkSaveLoging = logingService.SaveLoging(affairs, checkUser);
            if (!checkSave||!checkSaveLoging) {
                return Result.errorResult(Code.SERVER_ERROR);
            }
            return Result.okResult(Code.SUCCESS);
        }
//        boolean checkSaveLoging = logingService.SaveLoging(affairs, checkUser);
//        boolean checkSave = save(affairs);
//        if (!checkSave||!checkSaveLoging) {
//            return Result.errorResult(Code.SERVER_ERROR);
//        }
//        return Result.okResult(Code.SUCCESS);
        return Result.errorResult(Code.PARAM_INVALID);
    }

    @Override
    public Result RemoveAffairsArr(String[] ids, String memo) {
        if (BaseContext.getCurrentId()==null) {
            return Result.errorResult(Code.NEED_LOGIN);
        }
        Affairs affairs=new Affairs();
        for (String id :ids) {
            affairs.setAffairsId(Integer.valueOf(id));
            affairs.setAffairsMemo(memo);
            boolean checkUpdate = updateById(affairs);
            if (!checkUpdate) {
                return Result.errorResult(Code.SERVER_ERROR);
            }
        }
        return Result.okResult(Code.SUCCESS);
    }

    @Override
        public Result UserRemoveAffairs(Integer id, String memo) {
            Long userId = BaseContext.getCurrentId();
            User checkUser = userMapper.selectById(userId);
            if (checkUser==null||userId==null) {
                return Result.errorResult(Code.NEED_LOGIN);
            }
            Affairs affairs=new Affairs();
            affairs.setAffairsId(id);
            affairs.setAffairsMemo(memo);
            boolean checkUpdate = updateById(affairs);
            if (!checkUpdate) {
                return Result.errorResult(Code.SERVER_ERROR);
            }
            return Result.okResult(Code.SUCCESS);
        }

    @Override
        public Result GetAffairsActionList(GetAffairsListActionDto dto) {
            dto.checkParam();
            IPage<Affairs> page=new Page<>(dto.getPage(),dto.getSize());
            LambdaQueryWrapper<Affairs> wrapper=new LambdaQueryWrapper<>();
            wrapper.eq(Affairs::getAffairsCategory,dto.getCategory());

            if (dto.getType()!=null) {
                wrapper.eq(Affairs::getAffairsType,dto.getType());
            }
            if (dto.getDeviceId()!=null) {
                wrapper.eq(Affairs::getAffairsDeviceId,dto.getDeviceId());
            }
            if (dto.getDeviceTypeId()!=null) {
                wrapper.eq(Affairs::getAffairsDeviceTypeId,dto.getDeviceTypeId());
            }
            if (StringUtils.isNotBlank(dto.getName())) {
                wrapper.like(Affairs::getAffairsUserNick,dto.getName());
            }
            page=page(page,wrapper);
            Result result=new PageResponseResult(dto.getPage(),dto.getSize(), (int) page.getTotal());
            result.setData(page.getRecords());
            return result;
        }

    @Override
        public Result GetAffairsList(GetAffairsListDto dto) {
            dto.checkParam();
            IPage<Affairs> page=new Page<>(dto.getPage(),dto.getSize());
            LambdaQueryWrapper<Affairs> wrapper=new LambdaQueryWrapper<>();
            wrapper.eq(Affairs::getAffairsCategory,dto.getCategory());
            if (dto.getType()!=null) {
                wrapper.eq(Affairs::getAffairsType,dto.getType());
            }
            if (StringUtils.isNotBlank(dto.getDeviceName())) {
                wrapper.like(Affairs::getAffairsDeviceName,dto.getDeviceName());
            }
            if (dto.getDeviceTypeId()!=null) {
                wrapper.eq(Affairs::getAffairsDeviceTypeId,dto.getDeviceTypeId());
            }
            if (StringUtils.isNotBlank(dto.getUserName())) {
                wrapper.like(Affairs::getAffairsUserNick,dto.getUserName());
            }
            page=page(page,wrapper);
            Result result=new PageResponseResult(dto.getPage(),dto.getSize(), (int) page.getTotal());
            result.setData(page.getRecords());
            return result;
        }

        @Override
        public Result UserAffairsRepairTrue(Integer id) {
            User checkUser = userMapper.selectById(BaseContext.getCurrentId());
            if (checkUser==null) {
                return Result.errorResult(Code.NEED_LOGIN);
            }
            Affairs checkAffairs = getById(id);
            if (checkAffairs==null) {
                return Result.errorResult(Code.DATA_NOT_EXIST);
            }
            if (checkAffairs.getAffairsType()!=1) {
                return Result.errorResult(Code.NO_OPERATOR_AUTH);
            }
            checkAffairs.setAffairsType(3);
            checkAffairs.setAffairsEnd(3);
            boolean checkUpdate = updateById(checkAffairs);
            if (!checkUpdate) {
                return Result.errorResult(Code.SERVER_ERROR);
            }
            boolean checkLoging = logingService.UserNextTrue(checkUser, checkAffairs);
            if (!checkLoging) {
                return Result.errorResult(Code.SERVER_ERROR);
            }
            return Result.okResult(Code.SUCCESS);
        }

        @Override
        public Result UserAffairsRepairFalse(Integer id,String memo) {
            User checkUser = userMapper.selectById(BaseContext.getCurrentId());
            if (checkUser==null) {
                return Result.errorResult(Code.NEED_LOGIN);
            }
            Affairs checkAffairs = getById(id);
            if (checkAffairs==null) {
                return Result.errorResult(Code.DATA_NOT_EXIST);
            }
            if (checkAffairs.getAffairsType()!=1) {
                return Result.errorResult(Code.NO_OPERATOR_AUTH);
            }
            checkAffairs.setAffairsType(2);
            checkAffairs.setAffairsEnd(2);
            boolean checkUpdate = updateById(checkAffairs);
            if (!checkUpdate) {
                return Result.errorResult(Code.SERVER_ERROR);
            }
            return Result.okResult(Code.SUCCESS);
        }

        @Override
        public Result LeadershipAffairsRepairTrue(Integer id) {
            Leadership checkLeadership = leadershipMapper.selectById(BaseContext.getCurrentId());
            if (checkLeadership==null) {
                return Result.errorResult(Code.NEED_LOGIN);
            }
            Affairs checkAffairs = getById(id);
            if (checkAffairs==null) {
                return Result.errorResult(Code.DATA_NOT_EXIST);
            }
            if (checkAffairs.getAffairsType()==0) {
                checkAffairs.setAffairsType(1);
                checkAffairs.setAffairsEnd(1);
                boolean checkUpdate = updateById(checkAffairs);
                if (!checkUpdate) {
                    return Result.errorResult(Code.SERVER_ERROR);
                }
                return Result.okResult(Code.SUCCESS);

            }
            if (checkAffairs.getAffairsType()==2) {
                checkAffairs.setAffairsType(1);
                checkAffairs.setAffairsEnd(1);
                boolean checkUpdate = updateById(checkAffairs);
                if (!checkUpdate) {
                    return Result.errorResult(Code.SERVER_ERROR);
                }
                return Result.okResult(Code.SUCCESS);
            }
            return Result.errorResult(Code.NO_OPERATOR_AUTH);
        }

        @Override
        public Result LeadershipAffairsRepairFalse(Integer id, String memo) {
            Leadership checkLeadership = leadershipMapper.selectById(BaseContext.getCurrentId());
            if (checkLeadership==null) {
                return Result.errorResult(Code.NEED_LOGIN);
            }
            Affairs checkAffairs = getById(id);
            if (checkAffairs==null) {
                return Result.errorResult(Code.DATA_NOT_EXIST);
            }
            if (checkAffairs.getAffairsType()==0) {
                checkAffairs.setAffairsType(4);
                checkAffairs.setAffairsEnd(4);
                boolean checkUpdate = updateById(checkAffairs);
                if (!checkUpdate) {
                    return Result.errorResult(Code.SERVER_ERROR);
                }
                boolean checkLoging = logingService.LeadershipNextFalse(checkLeadership, checkAffairs, memo);
                if (!checkLoging) {
                    return Result.errorResult(Code.SERVER_ERROR);
                }
                return Result.okResult(Code.SUCCESS);

            }
            if (checkAffairs.getAffairsType()==2) {
                checkAffairs.setAffairsType(4);
                checkAffairs.setAffairsEnd(4);
                checkAffairs.setAffairsMemo(memo);
                boolean checkUpdate = updateById(checkAffairs);
                if (!checkUpdate) {
                    return Result.errorResult(Code.SERVER_ERROR);
                }
                boolean checkLoging = logingService.LeadershipNextFalse(checkLeadership, checkAffairs, memo);
                if (!checkLoging) {
                    return Result.errorResult(Code.SERVER_ERROR);
                }
                return Result.okResult(Code.SUCCESS);
            }
            return Result.errorResult(Code.NO_OPERATOR_AUTH);
        }

        @Override
        public Result LeadershipAffairsTrue(Integer id) {
            Leadership checkLeadership = leadershipMapper.selectById(BaseContext.getCurrentId());
            if (checkLeadership==null) {
                return Result.errorResult(Code.NEED_LOGIN);
            }
            Affairs checkAffairs = getById(id);
            if (checkAffairs==null) {
                return Result.errorResult(Code.DATA_NOT_EXIST);
            }
            System.out.println(checkAffairs.getAffairsType());
            if (checkAffairs.getAffairsType()!=0) {
                return Result.errorResult(Code.NO_OPERATOR_AUTH);
            }
            Device checkDevice = deviceService.getById(checkAffairs.getAffairsDeviceId());
            if (checkDevice==null) {
                return Result.errorResult(Code.DATA_NOT_EXIST);
            }
            if (checkAffairs.getAffairsCategory()==0||checkAffairs.getAffairsCategory()==3) {
                checkDevice.setDeviceInventory(checkDevice.getDeviceInventory()-checkAffairs.getAffairsNumber());
            }
            if (checkAffairs.getAffairsCategory()==6) {
                checkDevice.setDeviceInventory(checkDevice.getDeviceInventory()+checkAffairs.getAffairsNumber());
            }
            boolean checkDeviceUpdate = deviceService.updateById(checkDevice);
            checkAffairs.setAffairsType(3);
            checkAffairs.setAffairsEnd(3);
            boolean checkUpdate = updateById(checkAffairs);
            if (!checkUpdate) {
                return Result.errorResult(Code.SUCCESS);
            }
            if (checkAffairs.getAffairsCategory()==1) {
                Device getDevice = deviceService.getOne(Wrappers.<Device>lambdaQuery().eq(Device::getDeviceName, checkDevice.getDeviceName()).eq(Device::getDeviceDivisionId, checkAffairs.getAffairsEndUserDivisionId()));
                Device device=new Device();
                if (getDevice==null) {
                    BeanUtils.copyProperties(checkDevice,device);
                    device.setDeviceId(null);
                    device.setDeviceInventory(checkAffairs.getAffairsNumber());
                    device.setDeviceDivisionName(checkAffairs.getAffairsEndUserDivisionName());
                    device.setDeviceDivisionId(checkAffairs.getAffairsEndUserDivisionId());
                    device.setSaveTime(String.valueOf(LocalDateTime.now()));
                    boolean checkSave = deviceService.save(device);
                    if (!checkSave) {
                        return Result.errorResult(Code.SERVER_ERROR);
                    }
                    boolean checkSaveProcess = logingService.LeadershipNextTrue(checkLeadership, checkAffairs);
                    if (!checkDeviceUpdate||!checkSaveProcess) {
                        return Result.errorResult(Code.SERVER_ERROR);
                    }
                    return Result.okResult(Code.SUCCESS);
                }
                getDevice.setDeviceInventory(getDevice.getDeviceInventory()+checkAffairs.getAffairsNumber());
                boolean checkUpdateNew = deviceService.updateById(getDevice);
                if (!checkUpdateNew) {
                    return Result.errorResult(Code.SERVER_ERROR);
                }
            }
            boolean checkSaveProcess = logingService.LeadershipNextTrue(checkLeadership, checkAffairs);
            if (!checkDeviceUpdate||!checkSaveProcess) {
                return Result.errorResult(Code.SERVER_ERROR);
            }
            return Result.okResult(Code.SUCCESS);
        }

        @Override
        public Result LeadershipAffairsFalse(Integer id, String memo) {
            Leadership checkLeadership = leadershipMapper.selectById(BaseContext.getCurrentId());
            if (checkLeadership==null) {
                return Result.errorResult(Code.NEED_LOGIN);
            }
            Affairs checkAffairs = getById(id);
            if (checkAffairs==null) {
                return Result.errorResult(Code.DATA_NOT_EXIST);
            }
            if (checkAffairs.getAffairsType()!=0) {
                return Result.errorResult(Code.NO_OPERATOR_AUTH);
            }
            checkAffairs.setAffairsType(4);
            checkAffairs.setAffairsEnd(3);
            checkAffairs.setAffairsMemo(memo);
            boolean checkUpdate = updateById(checkAffairs);
            if (!checkUpdate) {
                return Result.errorResult(Code.SUCCESS);
            }
            boolean checkSaveProcess = logingService.LeadershipNextTrue(checkLeadership, checkAffairs);
            if (!checkSaveProcess) {
                return Result.errorResult(Code.SERVER_ERROR);
            }
            return Result.okResult(Code.SUCCESS);
        }

}




